
package StoryStageVisible

import(
	"fmt"
	"sanguo/node/table/excel"
	"sanguo/node/table/excel/table"
	"strconv"

	"github.com/tealeg/xlsx"
)

func readInt32(v string) int32 {
	return int32(readFloat(v))
}

func readFloat(v string) float64 {
	vv,err := strconv.ParseFloat(v,64)
	if nil != err {
		panic(err)
	}
	return vv
}

func readBool(str string) bool {
    b, err := strconv.ParseBool(str)
	if nil != err {
		panic(err)
	}
    return b
}

func readStr(v string) string {
	return fmt.Sprintf("%s",v)
}

type Limit_ struct {
	StoryStage int32
	Min int32
	Max int32
}

type StoryStageVisible struct {
	MapID int32
	Limit []Limit_
}

func readLimit(value string) []Limit_ {

	Limit := make([]Limit_,0)
	r := excel.Split(value,";,")

	for _,v := range(r) {
		Limit_ := Limit_{}

		Limit_.StoryStage = readInt32(v[0])

		Limit_.Min = readInt32(v[1])

		Limit_.Max = readInt32(v[2])
	
		Limit = append(Limit,Limit_)
	}	 	
	return Limit
}

type Table struct {
	MapIDMap map[int32]*StoryStageVisible
	tmpMapIDMap map[int32]*StoryStageVisible
	xlsxName string
}

var Table_ Table

func getIdxByName(names *xlsx.Row, name string) int {
	for i, v := range names.Cells {
		if v.String() == name {
			return i
		}
	}
	return -1
}

func init() {
	Table_.xlsxName = "StoryStageVisible.xlsx"
	Table_.MapIDMap = make(map[int32]*StoryStageVisible)
	Table_.tmpMapIDMap = make(map[int32]*StoryStageVisible)

	table.AddTable(&Table_)
}


func (this *Table) Load(path string) {

	excel.EmptyFunc()

    xlFile, err := xlsx.OpenFile(path + this.xlsxName)
    if err != nil {
		panic(err)
    }

    sheet := xlFile.Sheets[0]
    names := sheet.Rows[1]
    rows  := sheet.Rows[3:]

    for _,row := range(rows) {
    	if len(row.Cells) > 0 && row.Cells[0].String() != "" {
    		ele := &StoryStageVisible{}
    	
		ele.MapID = readInt32(row.Cells[getIdxByName(names, "MapID")].String())
		this.tmpMapIDMap[ele.MapID] = ele
		ele.Limit = readLimit(row.Cells[getIdxByName(names, "Limit")].String())

		}
    }

    this.MapIDMap = this.tmpMapIDMap
 
}

